﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class ReportIndicatori : SessionPageBase
{
    public string monthStart = "";
    public string monthEnd = "";
    public string monthStartI = "";
    public string monthEndI = "";
    public string year = "";
    public string yearI = "";
    HtmlGenericControl htmlControl = new HtmlGenericControl();
    private bool hideColumns;

    public bool HideColumns
    {
        get { return hideColumns; }
        set { hideColumns = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!string.IsNullOrEmpty(Request.QueryString["monthStart"].ToString()))
            {
                monthStart = Request.QueryString["monthStart"].ToString();
            }

            if (!string.IsNullOrEmpty(Request.QueryString["monthEnd"].ToString()))
            {
                monthEnd = Request.QueryString["monthEnd"].ToString();
            }

            if (!string.IsNullOrEmpty(Request.QueryString["monthStartI"].ToString()))
            {
                monthStartI = Request.QueryString["monthStartI"].ToString();
            }

            if (!string.IsNullOrEmpty(Request.QueryString["monthEndI"].ToString()))
            {
                monthEndI = Request.QueryString["monthEndI"].ToString();
            }

            if (!string.IsNullOrEmpty(Request.QueryString["year"].ToString()))
            {
                year = Request.QueryString["year"].ToString();
            }

            if (!string.IsNullOrEmpty(Request.QueryString["yearDateI"].ToString()))
            {
                yearI = Request.QueryString["yearDateI"].ToString();
            }

            DataSet ds_HideColumns = Hipocrate.DB.DBConnection.GetData(@"select value from Global where code = 'INDM'");
            HideColumns = Convert.ToBoolean(ds_HideColumns.Tables[0].Rows[0]["VALUE"]);
            

            DataSet ds_ReportInd = Hipocrate.DB.DBConnection.GetData("Indicatori", new string[] { "yearCode", "sMonth", "eMonth" }, new object[] { year, Convert.ToInt32(monthStartI), Convert.ToInt32(monthEndI) });
            DataSet ds_Contracted = Hipocrate.DB.DBConnection.GetData(string.Format(@"select convert(varchar(10),round(isnull(cvh.Contracted_Value,0),2)) as cv, cvh.code_indicator, cv.name_indicator
				FROM Contracted_Value_History cvh LEFT JOIN Contracted_Values cv on cv.Code_Indicator = cvh.Code_Indicator
				WHERE (cvh.StartDate IS NULL or cvh.StartDate <= '{0}-{1}-1') AND (cvh.EndDate IS NULL or cvh.EndDate >= '{0}-{1}-28')
				ORDER BY cvh.code_indicator", year, monthStartI));
            DataTable tblContracted = ds_Contracted.Tables[0];

            htmlControl.Controls.Add(new LiteralControl("<table border=\"1\" class=\"clsTable\" cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">"));

            htmlControl.Controls.Add(new LiteralControl("<tr class=\"clsBlack\">"));
            if (!HideColumns)
            {
                htmlControl.Controls.Add(new LiteralControl("<td rowspan=\"2\" style=\"font-size: small\">Nr.<br />crt.</td>"));
                htmlControl.Controls.Add(new LiteralControl("<td align=\"center\" class=\"style7\" rowspan=\"2\" style=\"font-size: small\">INDICATORI/CRITERII DE PERFORMANTA</td>"));
                htmlControl.Controls.Add(new LiteralControl("<td align=\"center\" class=\"style4\" colspan=\"2\" style=\"font-size: small\">VALOARE INDICATOR</td>"));
                htmlControl.Controls.Add(new LiteralControl("<td align=\"center\" rowspan=\"2\" style=\"font-size: small\">GRAD DE<br />REALIZARE</td>"));
                htmlControl.Controls.Add(new LiteralControl("<td align=\"center\" rowspan=\"2\" style=\"font-size: small\">PUNCTAJ<br />ACORDAT</td>"));
                htmlControl.Controls.Add(new LiteralControl("</tr>"));

                htmlControl.Controls.Add(new LiteralControl("<tr class=\"clsBlack\">"));
                htmlControl.Controls.Add(new LiteralControl("<td align=\"center\" class=\"style5\" style=\"font-size: small\">asumata<br />prin contract</td>"));
                htmlControl.Controls.Add(new LiteralControl("<td align=\"center\" class=\"style6\" style=\"font-size: small\">realizata</td>"));
                htmlControl.Controls.Add(new LiteralControl("</tr>"));
            }
            else
            {
                htmlControl.Controls.Add(new LiteralControl("<td></td>"));
                htmlControl.Controls.Add(new LiteralControl("<td></td>"));
                htmlControl.Controls.Add(new LiteralControl("<td align=\"center\" class=\"style6\" style=\"font-size: small\">Valoare Realizata</td>"));
                htmlControl.Controls.Add(new LiteralControl("</tr>"));
            }

            char prevGroupId = ' ';
            double rptInd = 0.00f;
            double indFac = 0.00f;
            for (int i = 0; i < tblContracted.Rows.Count; ++i)
            {
                if (tblContracted.Rows[i]["Code_Indicator"].ToString()[0] != prevGroupId)
                {
                    prevGroupId = tblContracted.Rows[i]["Code_Indicator"].ToString()[0];
                    htmlControl.Controls.Add(new LiteralControl("<tr class=\"clsBlack\">"));
                    switch (prevGroupId)
                    {
                        case 'A':
                            htmlControl.Controls.Add(new LiteralControl("<td align=\"center\" colspan=\"6\" style=\"font-size: small\">A. INDICATORI DE MANAGMENT AL RESURSELOR UMANE</td>"));
                            break;
                        case 'B':
                            htmlControl.Controls.Add(new LiteralControl("<td align=\"center\" colspan=\"6\" style=\"font-size: small\">B. INDICATORI DE UTILIZARE A SERVICIILOR</td>"));
                            break;
                        case 'C':
                            htmlControl.Controls.Add(new LiteralControl("<td align=\"center\" colspan=\"6\" style=\"font-size: small\">C. INDICATORI ECONOMICO - FINANCIARI</td>"));
                            break;
                        case 'D':
                            htmlControl.Controls.Add(new LiteralControl("<td align=\"center\" colspan=\"6\" style=\"font-size: small\">D. INDICATORI DE CALITATE</td>"));
                            break;
                    }

                    htmlControl.Controls.Add(new LiteralControl("</tr>"));
                }

                htmlControl.Controls.Add(new LiteralControl("<tr class=\"clsBlack\">"));

                htmlControl.Controls.Add(new LiteralControl("<td>" + (i + 1) + "</td>"));
                htmlControl.Controls.Add(new LiteralControl("<td>" + tblContracted.Rows[i]["name_indicator"].ToString() + "</td>"));

                if (!HideColumns)
                {
                    object cValue = tblContracted.Rows[i]["cv"];
                    htmlControl.Controls.Add(new LiteralControl("<td>" + cValue.ToString() + "</td>"));
                    if (ds_ReportInd.Tables[i].Rows[0][0] != System.DBNull.Value)
                    {
                        rptInd = Convert.ToDouble(ds_ReportInd.Tables[i].Rows[0][0]);
                        htmlControl.Controls.Add(new LiteralControl("<td>" + rptInd.ToString("0.00") + "</td>"));
                    }
                    else
                    {
                        rptInd = 0.00f;
                        htmlControl.Controls.Add(new LiteralControl("<td>" + "0.00" + "</td>"));
                    }
                    int MAX_VALUE = 100000;
                    decimal actualValue = Convert.ToDecimal(rptInd);
                    decimal relativeValue = Convert.ToDecimal(cValue) == 0 ?
                        0 //actualValue == 0 ? 0 : MAX_VALUE
                        : actualValue / Convert.ToDecimal(cValue) * 10000;
                    htmlControl.Controls.Add(new LiteralControl("<td>" + relativeValue.ToString("0.00") + "</td>"));
                    DataSet ds_Punctaj = Hipocrate.DB.DBConnection.GetData("select *  from PUNCTAJ where numorder=" + (i + 1).ToString() + " and " + relativeValue.ToString("0.00").Replace(",", ".") + " between [min] and  isnull([max]," + MAX_VALUE + ")");
                    if (ds_Punctaj.Tables[0].Rows.Count > 0)
                        htmlControl.Controls.Add(new LiteralControl("<td>" + ds_Punctaj.Tables[0].Rows[0][2].ToString() + "</td>"));
                }
                else
                {
                    if (ds_ReportInd.Tables[i].Rows[0][0] != System.DBNull.Value)
                    {
                        rptInd = Convert.ToDouble(ds_ReportInd.Tables[i].Rows[0][0]);
                        htmlControl.Controls.Add(new LiteralControl("<td>" + rptInd.ToString("0.00") + "</td>"));
                    }
                    else
                    {
                        rptInd = 0.00f;
                        htmlControl.Controls.Add(new LiteralControl("<td>" + "0.00" + "</td>"));
                    }
                }

                htmlControl.Controls.Add(new LiteralControl("</tr>"));
            }

            htmlControl.Controls.Add(new LiteralControl("</table><br/>"));

            if (HideColumns)
            {
                string sqlFactorIndicator = string.Format(@"select I.TYPEID,name,sum(result) as sumR from IND_FACTORS i inner join IND_REPORT_FACTORS ii on i.id=ii.idfactor and ii.valid=1
                    WHERE {0} <= ii.IDMONTH  AND ii.IDMONTH <= {1} AND ii.IDYEAR = {2}
                    group by I.TYPEID,name ORDER BY I.TYPEID desc", monthStartI, monthEndI, yearI);

                DataSet ds_FactorIndicator = Hipocrate.DB.DBConnection.GetData(sqlFactorIndicator);
                DataTable tblFactorIndicator = ds_FactorIndicator.Tables[0];

                htmlControl.Controls.Add(new LiteralControl("<table border=\"1\" class=\"clsTable\" cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">"));

                htmlControl.Controls.Add(new LiteralControl("<tr class=\"clsBlack\">"));
                htmlControl.Controls.Add(new LiteralControl("<td style=\"font-size: small\">Nr.<br />crt.</td>"));
                htmlControl.Controls.Add(new LiteralControl("<td align=\"center\" class=\"style7\" style=\"font-size: small\">Nume Indicatori</td>"));
                htmlControl.Controls.Add(new LiteralControl("<td align=\"center\" class=\"style4\" style=\"font-size: small\">Valoare Indicator</td>"));
                htmlControl.Controls.Add(new LiteralControl("</tr>"));

                for (int i = 0; i < tblFactorIndicator.Rows.Count; ++i)
                {

                    indFac = Convert.ToDouble(tblFactorIndicator.Rows[i]["sumR"].ToString());
                    htmlControl.Controls.Add(new LiteralControl("<tr class=\"clsBlack\">"));
                    htmlControl.Controls.Add(new LiteralControl("<td>" + (i + 1) + "</td>"));
                    htmlControl.Controls.Add(new LiteralControl("<td align=\"left\" class=\"style7\" style=\"font-size: small\">" + tblFactorIndicator.Rows[i]["name"].ToString() + "</td>"));
                    htmlControl.Controls.Add(new LiteralControl("<td align=\"center\" class=\"style7\" style=\"font-size: small\">" + indFac.ToString("0.00") + "</td>"));
                    htmlControl.Controls.Add(new LiteralControl("</tr>"));
                }


                htmlControl.Controls.Add(new LiteralControl("</table>"));

            }

            phReport.Controls.Add(htmlControl);
        }

        catch (Exception ex)
        {
            output.Text = "Eroare! " + ex.Message + "<!--" + ex.ToString() + "-->";
            //this.SetMessage(ex.Message, true);
        }
    }
}

